﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using System.Management;

namespace ERP.Data
{
    public class Common
    {
        public Common()
        {
            //
            // TODO: Add constructor logic here
            //
        }

      
        public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
        {
            var dtReturn = new DataTable();

             // column names 
             PropertyInfo[] oProps = null;

             if (varlist == null) return dtReturn;

             foreach (T rec in varlist)
             {
                  // Use reflection to get property names, to create table, Only first time, others  will follow 
                  if (oProps == null)
                  {
                       oProps = ((Type)rec.GetType()).GetProperties();
                       foreach (var pi in oProps)
                       {
                           var colType = pi.PropertyType;

                            if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                             {
                                 colType = colType.GetGenericArguments()[0];
                             }

                            dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                       }
                  }

                  var dr = dtReturn.NewRow();

                  //foreach (var pi in oProps)
                  //{
                  //     dr[pi.Name] = pi.GetValue(rec, null) == null ?DBNull.Value :pi.GetValue
                  //     (rec,null);
                  //}

                  foreach (var pi in oProps)
                  {
                      dr[pi.Name] = pi.GetValue(rec, null) ?? DBNull.Value;
                  }

                  dtReturn.Rows.Add(dr);
             }
             return dtReturn;
        }

        //#region cấp mã chứng từ

        ////them
        //public SalesSttEntity loadCapSoById()
        //{
        //    var data = new LinqMetaData();
        //    var result = from a in data.SalesStt
        //                 where a.IdNgay == DateTime.Now.ToString("yyMM")
        //                 select a;
        //    if (result.ToList().Count() > 0)
        //    {
        //        return result.Single();
        //    }
        //    else
        //    {
        //        SalesSttEntity stt = new SalesSttEntity
        //        {
        //            IdNgay  = DateTime.Now.ToString("yyMM"),
        //            SttPc = 0,
        //            SttPn = 0,
        //            SttPt = 0,
        //            SttPtra = 0,
        //            SttPxk = 0,
        //            SttPnk = 0
                  
        //        };
        //        stt.Save();
        //        return stt;
        //    }
        //}

        //// update
        //public bool UpdateStt(SalesSttEntity entity)
        //{
        //    return entity.Save();
        //}


        ////them
        //public SalesSttHdEntity loadSoHd()
        //{
        //    var data = new LinqMetaData();
        //    var result = from a in data.SalesSttHd
        //                 where a.IdNgay == DateTime.Now.ToString("yyMMdd")
        //                 select a;
        //    if (result.ToList().Count() > 0)
        //    {
        //        return result.Single();
        //    }
        //    else
        //    {
        //        SalesSttHdEntity stt = new SalesSttHdEntity
        //        {
        //            IdNgay = DateTime.Now.ToString("yyMMdd"),
        //            SttHd = 0

        //        };
        //        stt.Save();
        //        return stt;
        //    }
        //}

        //// update
        //public bool UpdateSoHd(SalesSttHdEntity entity)
        //{
        //    return entity.Save();
        //}

        //#endregion

      
    }
}
